const state = {
  deviceList: []
}

const mutations = {
  ADD_DEVICE: (state, device) => {
    let have = false
    for (let i = 0; i < state.deviceList.length; i++) {
      if (state.deviceList[i].deviceCore === device.deviceCore) {
        have = true
        break
      }
    }
    if (!have) {
      state.deviceList.unshift(device)
    }
  },
  REMOVE_DEVICE: (state, device) => {
    for (let i = 0; i < state.deviceList.length; i++) {
      if (state.deviceList[i].deviceCore === device.deviceCore) {
        state.deviceList.splice(i, 1)
        break
      }
    }
  }
}

const actions = {
  addDevice({ commit }, device) {
    commit('ADD_DEVICE', device)
  },
  removeDevice({ commit }, device) {
    commit('REMOVE_DEVICE', device)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
